package gui;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import client.ClientConsole;

import logic.Librarian;
import logic.Message;
import logic.Reader;

/**
 * Show all reader, worker details that had been inputed to the system 
 */
public class ViewWorkesrReadersForm extends JPanel implements ActionListener{
	/**  GUI Panel used for Design*/
	private JPanel mainPanel,centerPanel,bottomPanel,readerPanel,workerPanel;
	private JButton back; 
	/** shows feedback form the server*/
	private JLabel error;
	
	GridBagConstraints gbc = new GridBagConstraints();
	/** message that has been received from the server*/
	Message msg;
	/** holds all reader and worker details*/
	Object[] o=null;
	
	/**
	 * Default constructor-Builds the view worker/reader form
	 * shows all details of readers and workers
	 */
	public ViewWorkesrReadersForm()
	{
		super();
		Reader[] readerCards;
		Librarian[] librarianDetails;
		int index=0;
		int i=2;
		setLayout(new BorderLayout());
		getDetails();
		// if the are not details to be shown
		if (o!=null)
		{
			mainPanel=new JPanel();
			mainPanel.setLayout(new BorderLayout());
			centerPanel=new JPanel();
			centerPanel.setLayout(new BoxLayout( centerPanel, BoxLayout.Y_AXIS ));
			readerPanel=new JPanel(new GridBagLayout());
			
			JLabel rcards=new JLabel("Reader Cards:");
			gbc = new GridBagConstraints();
			gbc.gridx=0;
			gbc.gridy=0;
			gbc.gridwidth=5;
			gbc.insets= new Insets(7,7,7,7);
			readerPanel.add(rcards,gbc);
			JLabel username=new JLabel("Username");
			gbc = new GridBagConstraints();
			gbc.gridx=0;
			gbc.gridy=1;
			gbc.insets= new Insets(7,7,7,7);
			readerPanel.add(username,gbc);
			JLabel password=new JLabel("Password");
			gbc = new GridBagConstraints();
			gbc.gridx=1;
			gbc.gridy=1;
			gbc.insets= new Insets(7,7,7,7);
			readerPanel.add(password,gbc);
			JLabel email=new JLabel("Email");
			gbc = new GridBagConstraints();
			gbc.gridx=2;
			gbc.gridy=1;
			gbc.insets= new Insets(7,7,7,7);
			readerPanel.add(email,gbc);
			JLabel cellphone=new JLabel("Cellphone");
			gbc = new GridBagConstraints();
			gbc.gridx=3;
			gbc.gridy=1;
			gbc.insets= new Insets(7,7,7,7);
			readerPanel.add(cellphone,gbc);
			JLabel status=new JLabel("Status");
			gbc = new GridBagConstraints();
			gbc.gridx=4;
			gbc.gridy=1;
			gbc.insets= new Insets(7,7,7,7);
			readerPanel.add(status,gbc);
			
			readerCards=(Reader[]) o[0];
			
			//show all details
			for (index=0;index<readerCards.length;index++)
			{
				username=new JLabel(readerCards[index].getUsername());
				gbc = new GridBagConstraints();
				gbc.gridx=0;
				gbc.gridy=i;
				gbc.insets= new Insets(7,7,7,7);
				readerPanel.add(username,gbc);
				password=new JLabel(readerCards[index].getPassword());
				gbc = new GridBagConstraints();
				gbc.gridx=1;
				gbc.gridy=i;
				gbc.insets= new Insets(7,7,7,7);
				readerPanel.add(password,gbc);
				email=new JLabel(readerCards[index].getEmail());
				gbc = new GridBagConstraints();
				gbc.gridx=2;
				gbc.gridy=i;
				gbc.insets= new Insets(7,7,7,7);
				readerPanel.add(email,gbc);
				cellphone=new JLabel(readerCards[index].getCellphone());
				gbc = new GridBagConstraints();
				gbc.gridx=3;
				gbc.gridy=i;
				gbc.insets= new Insets(7,7,7,7);
				readerPanel.add(cellphone,gbc);
				status=new JLabel(readerCards[index].getStatus());
				gbc = new GridBagConstraints();
				gbc.gridx=4;
				gbc.gridy=i++;
				gbc.insets= new Insets(7,7,7,7);
				readerPanel.add(status,gbc);
			}
			JLabel space=new JLabel("");
			gbc = new GridBagConstraints();
			gbc.gridx=0;
			gbc.gridy=i++;
			gbc.insets= new Insets(20,20,20,20);
			readerPanel.add(space,gbc);
			
			centerPanel.add(readerPanel);
			
			
			workerPanel=new JPanel(new GridBagLayout());
			
			JLabel wdetails=new JLabel("Librarian Details:");
			gbc = new GridBagConstraints();
			gbc.gridx=0;
			gbc.gridy=0;
			gbc.gridwidth=5;
			gbc.insets= new Insets(7,7,7,7);
			workerPanel.add(wdetails,gbc);
			username=new JLabel("Username");
			gbc = new GridBagConstraints();
			gbc.gridx=0;
			gbc.gridy=1;
			gbc.insets= new Insets(7,7,7,7);
			workerPanel.add(username,gbc);
			password=new JLabel("Password");
			gbc = new GridBagConstraints();
			gbc.gridx=1;
			gbc.gridy=1;
			gbc.insets= new Insets(7,7,7,7);
			workerPanel.add(password,gbc);
			email=new JLabel("Email");
			gbc = new GridBagConstraints();
			gbc.gridx=2;
			gbc.gridy=1;
			gbc.insets= new Insets(7,7,7,7);
			workerPanel.add(email,gbc);
			cellphone=new JLabel("Cellphone");
			gbc = new GridBagConstraints();
			gbc.gridx=3;
			gbc.gridy=1;
			gbc.insets= new Insets(7,7,7,7);
			workerPanel.add(cellphone,gbc);
			JLabel firstname=new JLabel("Firstname");
			gbc = new GridBagConstraints();
			gbc.gridx=4;
			gbc.gridy=1;
			gbc.insets= new Insets(7,7,7,7);
			workerPanel.add(firstname,gbc);
			JLabel lastname=new JLabel("LastName");
			gbc = new GridBagConstraints();
			gbc.gridx=5;
			gbc.gridy=1;
			gbc.insets= new Insets(7,7,7,7);
			workerPanel.add(lastname,gbc);
			
			librarianDetails=(Librarian[]) o[1];
			index=0;
			i=2;
			//shows all workers
			for (index=0;index<librarianDetails.length;index++)
			{
				username=new JLabel(librarianDetails[index].getUsername());
				gbc = new GridBagConstraints();
				gbc.gridx=0;
				gbc.gridy=i;
				gbc.insets= new Insets(7,7,7,7);
				workerPanel.add(username,gbc);
				password=new JLabel(librarianDetails[index].getPassword());
				gbc = new GridBagConstraints();
				gbc.gridx=1;
				gbc.gridy=i;
				gbc.insets= new Insets(7,7,7,7);
				workerPanel.add(password,gbc);
				email=new JLabel(librarianDetails[index].getEmail());
				gbc = new GridBagConstraints();
				gbc.gridx=2;
				gbc.gridy=i;
				gbc.insets= new Insets(7,7,7,7);
				workerPanel.add(email,gbc);
				cellphone=new JLabel(librarianDetails[index].getCellphone());
				gbc = new GridBagConstraints();
				gbc.gridx=3;
				gbc.gridy=i;
				gbc.insets= new Insets(7,7,7,7);
				workerPanel.add(cellphone,gbc);
				firstname=new JLabel(librarianDetails[index].getFirstname());
				gbc = new GridBagConstraints();
				gbc.gridx=4;
				gbc.gridy=i;
				gbc.insets= new Insets(7,7,7,7);
				workerPanel.add(firstname,gbc);
				lastname=new JLabel(librarianDetails[index].getLastname());
				gbc = new GridBagConstraints();
				gbc.gridx=5;
				gbc.gridy=i++;
				gbc.insets= new Insets(7,7,7,7);
				workerPanel.add(lastname,gbc);
			}
			space=new JLabel("");
			gbc = new GridBagConstraints();
			gbc.gridx=0;
			gbc.gridy=i++;
			gbc.insets= new Insets(20,20,20,20);
			workerPanel.add(space,gbc);
			
			centerPanel.add(readerPanel);
			centerPanel.add(workerPanel);
			
			mainPanel.add(centerPanel,BorderLayout.CENTER);
			
		}
		else
		{
			error.setText("No readers and workers to show");
			error.setVisible(true);
		}
		bottomPanel=new JPanel();
		
		
		back=new JButton("back");
		bottomPanel.add(back);
		back.addActionListener(this);
		error=new JLabel();
		error.setVisible(false);
		bottomPanel.add(error);
		mainPanel.add(bottomPanel,BorderLayout.SOUTH); 
		JScrollPane thePane = new JScrollPane(mainPanel);
		add(thePane,BorderLayout.CENTER);
		
		
		
	}

	/**
	 * receives all readers and workers details from the server
	 */
	private void getDetails() {
		
		msg=new Message ("Manager Get Details",null);
		ClientConsole.HandleMessageFromGui(msg);
		msg=(Message)ClientConsole.client.getMessage();
		if (msg.getCommand().equals("Returned Readers and Workers"))
		{
			if (((Object[]) msg.getObjects()).length>0)
				o=(Object[]) msg.getObjects();
		}
	}

	@Override
	public void actionPerformed(ActionEvent ae) {
		if (ae.getSource()==back)
		{
			this.setVisible(false);
			ClientConsole.aFrame.setContentPane(new LibraryManagerWindow());
		}
		
	}
}
